<?php

namespace App\Http\Controllers;

use App\Model\Download;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Utils\Page;

class SoftController extends Controller
{
    protected function detail(Request $request){
        preg_match('/(^\d+)\.html/i', $request->id, $newsid);
        $newsid = (int)$newsid[1];
        $softContent = Cache::remember(__FUNCTION__ . '_softContent_' . $newsid, env('SHADUSOFT_CACHE_LIFETIME'), function () use ($newsid) {
            $info = Download::leftjoin('ecms_download_data_1 as d1','d1.id','=','ecms_download.id')
                ->where('ecms_download.id',$newsid)
                ->first();
            return $info;
        });
        if(empty($softContent))
            abort(404);
        return view('Soft.detail',compact('softContent'));
    }
    protected function list(Request $request){
        $page = (int) $request->page;
        $request->merge(['page' => $page]);
        $list = Cache::remember(__FUNCTION__ . '_softContent_' . $page, env('SHADUSOFT_CACHE_LIFETIME'), function (){
            return Download::leftjoin('ecms_download_data_1 as d1','d1.id','=','ecms_download.id')
                ->select('ecms_download.*','d1.*')
                ->orderByDesc('ecms_download.newstime')
                ->paginate(10);
        });
        if($list->lastPage() < $page) {
            abort(404);
        }
//        dd($new_list);
        $show_page = new Page($list->total(), $list->perPage(), 'soft', $page, ''); //分页信息
        return view('Soft.list',compact('list','show_page'));
    }
}
